package com.sixth.docker.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sixth.core.entity.Scheduling;
import com.sixth.core.entity.User;
import com.sixth.core.feign.UserFeign;
import com.sixth.core.vo.R;
import com.sixth.docker.mapper.SchedulingMapper;
import com.sixth.docker.service.SchedulingServiceApp;
import com.sixth.docker.vo.SchedulingDto;
import com.sixth.docker.vo.SchedulingVo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import java.util.*;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 赵庆龙
 * @create: 2024-08-16 09:26
 **/
@Service
public class SchedulingServiceImplApp implements SchedulingServiceApp {
    @Autowired
    private SchedulingMapper schedulingMapper;
    @Autowired
    private UserFeign userFeign;

    public R<Map<String, Object>> selectScheduling(SchedulingVo schedulingVo) {
        //根据部门id以及员工id查询值班医生的信息。
        Map<String, Object> params = new HashMap<>();
        params.put("userId", schedulingVo.getUserId());
        params.put("deptId", schedulingVo.getDeptId());
        //远程调用其他微服务的接口
        R<List<User>> schedulingUser = userFeign.findSchedulingUser(params);
        List<User> users = schedulingUser.getData();

        return findResult(users, schedulingVo);
    }


    private R<Map<String, Object>> findResult(List<User> users, SchedulingVo schedulingVo) {
        // 获取当前日期
        Date date = new Date();
        if (schedulingVo.getQueryDate() != null) {
            date = DateUtil.parse(schedulingVo.getQueryDate(), "yyyy-MM-dd");
        }

        // 设置 beginDate 为当前日期
        DateTime beginDate = DateUtil.beginOfDay(date);

        // 计算两周后的结束日期（包括当前日期在内的14天）
        DateTime endDate = DateUtil.offsetDay(beginDate, 13);

        // 设置 schedulingVo 的开始和结束日期
        schedulingVo.setBeginDate(DateUtil.format(beginDate, "yyyy-MM-dd"));
        schedulingVo.setEndDate(DateUtil.format(endDate, "yyyy-MM-dd"));


        // 根据vo查询值班表
        List<Scheduling> schedulings = selectSchedulingByParams(schedulingVo);

        // 创建一个集合DTO对象
        List<SchedulingDto> schedulingDtos = new ArrayList<>();
        for (User user : users) {
            Long userId = user.getUserId();
            // 表示上午值班
            SchedulingDto schedulingDto1 = new SchedulingDto(userId, user.getDeptId(), "1", initMap(beginDate));
            // 表示下午值班
            SchedulingDto schedulingDto2 = new SchedulingDto(userId, user.getDeptId(), "2", initMap(beginDate));
            // 表示晚上值班
            SchedulingDto schedulingDto3 = new SchedulingDto(userId, user.getDeptId(), "3", initMap(beginDate));

            for (Scheduling scheduling : schedulings) {
                // 值班类型 门诊 急诊
                String schedulingType = scheduling.getSchedulingType();
                // 值班时间段 上午 下午 晚上
                String subsectionType = scheduling.getSubsectionType();
                // 值班日期
                String schedulingDay = scheduling.getSchedulingDay();
                // 值班医生的id
                Integer userId1 = scheduling.getUserId();
                if ((userId + "").equals(userId1 + "")) {
                    switch (subsectionType) {
                        case "1":
                            schedulingDto1.getRecord().put(schedulingDay, schedulingType);
                            break;
                        case "2":
                            schedulingDto2.getRecord().put(schedulingDay, schedulingType);
                            break;
                        case "3":
                            schedulingDto3.getRecord().put(schedulingDay, schedulingType);
                            break;
                    }
                }
            }
            schedulingDto1.setSchedulingType(schedulingDto1.getRecord().values());
            schedulingDto2.setSchedulingType(schedulingDto2.getRecord().values());
            schedulingDto3.setSchedulingType(schedulingDto3.getRecord().values());
            schedulingDtos.add(schedulingDto1);
            schedulingDtos.add(schedulingDto2);
            schedulingDtos.add(schedulingDto3);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("schedulingDtos", schedulingDtos);
        result.put("startTimeThisWeek", DateUtil.format(beginDate, "MM-dd"));
        result.put("endTimeThisWeek", DateUtil.format(endDate, "MM-dd"));
        result.put("labelNames", getLables(beginDate));
        return R.ok(result);
    }

    // 更新 initMap 方法以包含未来两周的日期
    private Map<String, String> initMap(DateTime beginDate) {
        Map<String, String> map = new LinkedHashMap<>();
        for (int i = 0; i < 14; i++) { // 扩展至14天
            DateTime dateTime = DateUtil.offsetDay(beginDate, i);
            map.put(DateUtil.format(dateTime, "yyyy-MM-dd"), "");
        }
        return map;
    }

    private List<String> getLables(DateTime beginDate) {
        List<String> labelNames = new ArrayList<>();
        int startDayOfWeek = (DateUtil.dayOfWeek(beginDate) + 5) % 7; // 获取当天是星期几，调整周日为0，周一为1

        for (int i = 0; i < 14; i++) { // 扩展至14天
            DateTime dateTime = DateUtil.offsetDay(beginDate, i);
            int currentDayOfWeek = (startDayOfWeek + i) % 7; // 计算当前日期是星期几
            String week = DateUtil.format(dateTime, "MM-dd") + getWeek(currentDayOfWeek);
            labelNames.add(week);
        }
        return labelNames;
    }

    private String getWeek(int i) {
        String week = "";
        switch (i) {
            case 0:
                week = "周一";
                break;
            case 1:
                week = "周二";
                break;
            case 2:
                week = "周三";
                break;
            case 3:
                week = "周四";
                break;
            case 4:
                week = "周五";
                break;
            case 5:
                week = "周六";
                break;
            case 6:
                week = "周日";
                break;
        }
        return week;
    }

    //设置一周的上午 下午 晚上的默认值班



    private List<Scheduling> selectSchedulingByParams(SchedulingVo schedulingVo) {
        System.out.println(schedulingVo);
        QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(schedulingVo.getUserId())) {
            wrapper.eq("user_id", schedulingVo.getUserId());
        }
        if (Objects.nonNull(schedulingVo.getDeptId())) {
            wrapper.eq("dept_id", schedulingVo.getDeptId());
        }
        if (Objects.nonNull(schedulingVo.getBeginDate())) {
            wrapper.between("scheduling_day", schedulingVo.getBeginDate(), schedulingVo.getEndDate());
        }
        return schedulingMapper.selectList(wrapper);
    }

}


